package com.maimao.user.controller;

import com.maimao.core.model.auth.AuthUser;
import com.maimao.core.model.resp.Result;
import com.maimao.model.user.Foot;
import com.maimao.user.controller.base.BaseController;
import com.maimao.user.service.FootService;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 我的足迹 使用redis存储
 *
 * @author MaoLin Wang
 * @date 2021/1/26 11:56 上午
 */
@RestController
@RequestMapping("/web/foot")
public class FootController extends BaseController {

    @Autowired
    private FootService footService;

    /**
     * 添加足迹
     *
     * @return
     */
    @PostMapping("/add-foot")
    public Result<?> saveFoot(@RequestBody Map<String, String> data) {
        String goodsId = data.get("goodsId");
        if (StringUtils.isEmpty(goodsId)) {
            return Result.notValid();
        }
        Long goodsIdLong = Long.valueOf(goodsId);
        if (goodsIdLong <= 0) {
            return Result.notValid();
        }
        AuthUser authUser = checkAndGetAuthUser();
        footService.save(authUser.getId2Long(), goodsIdLong);
        return Result.ok();
    }

    @GetMapping("/foot-list")
    public Result<?> userFootList() {
        AuthUser authUser = checkAndGetAuthUser();
        List<Foot> footList = footService.userFootData(authUser.getId2Long());
        return Result.okData(footList);
    }

}
